{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('0.24.2', '1.16.3')"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "pd.__version__, np.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>company_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>alice</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>bob</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>charlie</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>david</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id     name  company_id\n",
       "0   1    alice           1\n",
       "1   2      bob           2\n",
       "2   3  charlie           1\n",
       "3   4    david           2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_employees = pd.DataFrame({\n",
    "    'id':[1,2,3,4],\n",
    "    'name':['alice','bob','charlie','david'],\n",
    "    'company_id':[1,2,1,2]\n",
    "})\n",
    "df_employees"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>bell labs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>xerox</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id       name\n",
       "0   1  bell labs\n",
       "1   2      xerox"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_companies = pd.DataFrame({\n",
    "    'id':[1,2],\n",
    "    'name': ['bell labs', 'xerox']\n",
    "})\n",
    "df_companies"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## merge on multiple columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>year</th>\n",
       "      <th>salary</th>\n",
       "      <th>company_id</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>alice</td>\n",
       "      <td>1980</td>\n",
       "      <td>30000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>bob</td>\n",
       "      <td>1980</td>\n",
       "      <td>40000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>3</td>\n",
       "      <td>charlie</td>\n",
       "      <td>1980</td>\n",
       "      <td>35000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>4</td>\n",
       "      <td>david</td>\n",
       "      <td>1980</td>\n",
       "      <td>45000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>alice</td>\n",
       "      <td>1981</td>\n",
       "      <td>30000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>bob</td>\n",
       "      <td>1981</td>\n",
       "      <td>41000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3</td>\n",
       "      <td>charlie</td>\n",
       "      <td>1981</td>\n",
       "      <td>40000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>4</td>\n",
       "      <td>david</td>\n",
       "      <td>1981</td>\n",
       "      <td>45000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id     name  year  salary  company_id\n",
       "0   1    alice  1980   30000           1\n",
       "2   2      bob  1980   40000           2\n",
       "4   3  charlie  1980   35000           1\n",
       "6   4    david  1980   45000           2\n",
       "1   1    alice  1981   30000           1\n",
       "3   2      bob  1981   41000           2\n",
       "5   3  charlie  1981   40000           1\n",
       "7   4    david  1981   45000           2"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_employees_sal = pd.DataFrame({\n",
    "    'id':[1,1,2,2,3,3,4,4],\n",
    "    'name':['alice','alice','bob','bob','charlie','charlie','david','david'],\n",
    "    'year':[1980,1981,1980,1981,1980,1981,1980,1981],\n",
    "    'salary':[30000,30000,40000,41000,35000,40000,45000,45000],\n",
    "    'company_id':[1,1,2,2,1,1,2,2]\n",
    "})\n",
    "df_employees_sal.sort_values(by=['year','name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>name</th>\n",
       "      <th>year</th>\n",
       "      <th>revenue</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>bell labs</td>\n",
       "      <td>1980</td>\n",
       "      <td>1130000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>xerox</td>\n",
       "      <td>1980</td>\n",
       "      <td>5000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>bell labs</td>\n",
       "      <td>1981</td>\n",
       "      <td>1130000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "      <td>xerox</td>\n",
       "      <td>1981</td>\n",
       "      <td>500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id       name  year  revenue\n",
       "0   1  bell labs  1980  1130000\n",
       "2   2      xerox  1980  5000000\n",
       "1   1  bell labs  1981  1130000\n",
       "3   2      xerox  1981   500000"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_companies_rev = pd.DataFrame({\n",
    "    'id':[1,1,2,2],\n",
    "    'name':['bell labs','bell labs','xerox','xerox'],\n",
    "    'year':[1980,1981,1980,1981],\n",
    "    'revenue':[1130000,1130000,5000000,500000]\n",
    "})\n",
    "df_companies_rev.sort_values(by=['year','name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id_x</th>\n",
       "      <th>name_x</th>\n",
       "      <th>year</th>\n",
       "      <th>salary</th>\n",
       "      <th>company_id</th>\n",
       "      <th>id_y</th>\n",
       "      <th>name_y</th>\n",
       "      <th>revenue</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>alice</td>\n",
       "      <td>1980</td>\n",
       "      <td>30000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>bell labs</td>\n",
       "      <td>1130000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>3</td>\n",
       "      <td>charlie</td>\n",
       "      <td>1980</td>\n",
       "      <td>35000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>bell labs</td>\n",
       "      <td>1130000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>alice</td>\n",
       "      <td>1981</td>\n",
       "      <td>30000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>bell labs</td>\n",
       "      <td>1130000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>charlie</td>\n",
       "      <td>1981</td>\n",
       "      <td>40000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>bell labs</td>\n",
       "      <td>1130000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2</td>\n",
       "      <td>bob</td>\n",
       "      <td>1980</td>\n",
       "      <td>40000</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>xerox</td>\n",
       "      <td>5000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>4</td>\n",
       "      <td>david</td>\n",
       "      <td>1980</td>\n",
       "      <td>45000</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>xerox</td>\n",
       "      <td>5000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2</td>\n",
       "      <td>bob</td>\n",
       "      <td>1981</td>\n",
       "      <td>41000</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>xerox</td>\n",
       "      <td>500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>4</td>\n",
       "      <td>david</td>\n",
       "      <td>1981</td>\n",
       "      <td>45000</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>xerox</td>\n",
       "      <td>500000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   id_x   name_x  year  salary  company_id  id_y     name_y  revenue\n",
       "0     1    alice  1980   30000           1     1  bell labs  1130000\n",
       "1     3  charlie  1980   35000           1     1  bell labs  1130000\n",
       "2     1    alice  1981   30000           1     1  bell labs  1130000\n",
       "3     3  charlie  1981   40000           1     1  bell labs  1130000\n",
       "4     2      bob  1980   40000           2     2      xerox  5000000\n",
       "5     4    david  1980   45000           2     2      xerox  5000000\n",
       "6     2      bob  1981   41000           2     2      xerox   500000\n",
       "7     4    david  1981   45000           2     2      xerox   500000"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.merge(\n",
    "    df_employees_sal,\n",
    "    df_companies_rev,\n",
    "    left_on=['year','company_id'],\n",
    "    right_on=['year','id']\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
